package com.hpin.mlxteacher.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.bean.ChildResult;
import com.hpin.mlxteacher.child.FamilyDetailsActivity;
import com.hpin.mlxteacher.child.WatchDetailActivity;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 * Created by Aaron on 15/7/29.
 */
public class ChildAdapter extends BaseAdapter {

    private List<ChildResult.childlist> tempList;
    private Context ctx;
    private DisplayImageOptions options;

    public ChildAdapter(Context ctx, List<ChildResult.childlist> tempList) {
        this.ctx = ctx;
        this.tempList = tempList;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_default)
                .showImageForEmptyUri(R.drawable.icon_default)
                .showImageOnFail(R.drawable.icon_default)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

    }

//    public void setTempList(List<ChildResult.childlist> tempList) {
//        this.tempList = tempList;
//    }

    @Override
    public int getCount() {
        return tempList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_child, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_headpic = (ImageView) convertView.findViewById(R.id.iv_headpic);

            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_distance = (TextView) convertView.findViewById(R.id.tv_distance);
            viewHolder.tv_introduce = (TextView) convertView.findViewById(R.id.tv_introduce);

            viewHolder.tv_baby_detail = (TextView) convertView.findViewById(R.id.tv_baby_detail);
            viewHolder.tv_watch_detail = (TextView) convertView.findViewById(R.id.tv_watch_detail);
            viewHolder.tv_call_watch = (TextView) convertView.findViewById(R.id.tv_call_watch);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_name.setText(tempList.get(position).name);
//        viewHolder.iv_headpic.setImageURI(Uri.parse(tempList.get(position).photo));
//        头像
        ImageLoader.getInstance().displayImage(tempList.get(position).picUrl, viewHolder.iv_headpic,
                options);
        viewHolder.tv_distance.setText(tempList.get(position).distance + "米");
        viewHolder.tv_introduce.setText(tempList.get(position).sex + "   " + tempList.get(position).year + "岁");
//        viewHolder.tv_introduce.setText(tempList.get(position).sex + "   " + tempList.get(position).age + "岁" + "   " + tempList.get(position).addressNow);
        viewHolder.tv_baby_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //详情
//                Intent intent = new Intent(ctx, ChildDetailActivity.class);
                Intent intent = new Intent(ctx, FamilyDetailsActivity.class);
                intent.putExtra("id", tempList.get(position).id);
                ctx.startActivity(intent);
            }
        });

        viewHolder.tv_watch_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //手表详情
                Intent intent = new Intent(ctx, WatchDetailActivity.class);

                if (tempList.get(position).device==null) {
                    Toast.makeText(ctx, "未获取到孩子手表信息!", Toast.LENGTH_SHORT).show();
                    return;
                }

                ChildResult.childlist child = tempList.get(position);

                intent.putExtra("child", child);

                ctx.startActivity(intent);
            }
        });

        viewHolder.tv_call_watch.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                final String phone = tempList.get(position).device.phoneNum;

                if (!CommonUtils.isPhone(phone)) {
                    Toast.makeText(ctx, "未获取到正确的手机号码!", Toast.LENGTH_SHORT).show();
                    return;
                }

                new AlertDialog.Builder(ctx).setMessage(phone).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //拨打电话
                        Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + phone));
                        ctx.startActivity(intent);


                    }
                }).show();
            }
        });

        return convertView;
    }

    private class ViewHolder {

        private ImageView iv_headpic;
        private TextView tv_name;
        private TextView tv_distance;
        private TextView tv_introduce;
        private TextView tv_baby_detail;
        private TextView tv_watch_detail;
        private TextView tv_call_watch;

    }
}
